﻿package com.zit.dao;

import java.util.List;

import org.hibernate.Session;

import com.zit.entity.Dept;
import com.zit.entity.Teacher;
import com.zit.service.TeacherService;
import com.zit.util.MD5;

/**
 * 教师管理DAO
 * 
 * @author 王冲
 * @createDate 2013-12-03
 */
@SuppressWarnings("unchecked")
public class TeacherDAO extends BaseDAO {
	
	/**
	 * 功能:用户登录
	 * @author 唐美兰
	 * @param teacher
	 * @param s
	 * @return
	 */
	public Teacher login(Teacher teacher,Session s){
		Object obj=s.createQuery("from Teacher t where t.loginName=:name and t.password=:password").setParameter("name", teacher.getLoginName()).setParameter("password", teacher.getPassword()).uniqueResult();
		return obj==null?null:(Teacher)obj;
	}
	
	public static void main(String[] args) {
		TeacherService teacherService=new TeacherService();
//		Teacher t=new Teacher("王冲", "wangchong", MD5.md5("0"), 0);
//		teacherService.save(t);
		Teacher t=new Teacher("董定卓", "laodong", MD5.md5("0"), 0);
		teacherService.save(t);
		Teacher t1=new Teacher("马立峰", "laoma", MD5.md5("0"), 0);
		teacherService.save(t1);
		Teacher t2=new Teacher("刘英", "laoliu", MD5.md5("0"), 1);
		teacherService.save(t2);
		Teacher t3=new Teacher("刘红印", "laoliu", MD5.md5("0"), 1);
		teacherService.save(t3);
	}
	/**
	 * 查询所有部门
	 * 
	 * @param s
	 * @return
	 */
	public List<Dept> selectdept(Session s) {
		return s.createQuery("from Dept").list();
	}
	
	/**
	 * 根据部门查询部门下面的教师
	 * 
	 * @param id
	 * @param s
	 * @return
	 */
	public List<Teacher> selectteacherid(Integer id, Session s) {
		return (List<Teacher>) s
				.createQuery(
						"from Teacher t join fetch t.dept where t.dept.id=:did and t.isDelete=0")
				.setParameter("did", id).list();
	}

	/**
	 * 查询所有老师
	 * 
	 * @param s
	 * @return
	 */
	public List<Teacher> selectteacher(Session s) {
		return (List<Teacher>) s.createQuery(
				"from Teacher t join fetch t.dept where t.isDelete=0").list();
	}
	
	/**
	 * 修改老师的部门
	 * 
	 * @param id
	 * @param s
	 */
	public void updateteacherdept(Integer deptid, Integer id, Session s) {
		s.createQuery("update Teacher t set t.dept.id=:did where t.id=:tid")
				.setParameter("did", deptid).setParameter("tid", id)
				.executeUpdate();
	}
	
	/**
	 * 根据id查询老师
	 * 
	 * @param s
	 * @return
	 */
	public Teacher idselectteacher(Session s,Integer tid) {
		return  (Teacher) s.createQuery(
				"from Teacher t join fetch t.dept where t.isDelete=0 and t.id=:tid").setParameter("tid", tid).uniqueResult();
	}

	/**
	 * 修改老师的部门
	 * 
	 * @param id
	 * @param s
	 */
	public void updateteacherdept(Teacher teacher, Session s) {
		s.createQuery("update Teacher t set t.dept.id=:did,t.name=:tname,t.isPro=:tispro where t.id=:tid")
				.setParameter("did", teacher.getDept().getId())
				.setParameter("tname", teacher.getName())
				.setParameter("tispro", teacher.getIsPro())
				.setParameter("tid", teacher.getId())
				.executeUpdate();
	}
	/**
	 * 根据id删除老师信息
	 * 
	 * @param id
	 * @param s
	 */
	public void deleteteacher(Integer id, Session s) {
		s.createQuery("update Teacher t set t.isDelete=1 where t.id=:tid")
				.setParameter("tid", id).executeUpdate();
	}

	/**
	 * 查询tid教师带的班级个数
	 * @param tid 教师编号 
	 * @param did 部门编号
	 * @param s   session
	 * @return
	 */
	public int countClasses(Integer tid, Integer did, Session s) {
		return Integer.valueOf(s.createQuery("select count(*) from Classes c where c.teacherByProTeacher.id=:tid or c.teacherByHeadTeacher.id=:tid and c.isDelete=0").setParameter("tid", tid).uniqueResult().toString());
	}

	/**
	 * @param id
	 * @param s
	 * @return
	 */
	public Teacher findByteaching(Integer id, Session s) {
		return (Teacher) s.createQuery("from Teacher t left join fetch t.classesesForHeadTeacher c1 left join fetch t.classesesForProTeacher c2 left join fetch t.dept d where t.id=:id").setParameter("id", id).uniqueResult();
	}

	public List<Teacher> findChangeTeaForDelTeacher(Teacher teacher, Session s) {
		return s.createQuery("from Teacher t where t.dept.id=:did and t.isDelete=0 and t.id<>:tid and t.isPro=:pro").setParameter("pro", teacher.getIsPro()).setParameter("tid", teacher.getId()).setParameter("did", teacher.getDept().getId()).list();
	}


}
